/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shopcart', [
    "ngResource",
    "ngStorage"
])
    //购物车
    .service('ShopcartService',['$resource','$localStorage','ShopMenuService','ShopService',function($resource,$localStorage,ShopMenuService,ShopService){
        var $storage = $localStorage.$default({
            cmtShopcart: {},
        });
        var shop_goods_list = $storage.cmtShopcart;

        //新增商品到购物车
        this.addgoods = function(shopid,cid,goodsid){

            ShopMenuService.addGood(shopid,cid,goodsid);

            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goods_list.length;i++){
                if(goods_list[i].cid == cid){
                    goodsitem = goods_list[i];
                    break;
                }
            }
            if(goodsitem == null){
                var detail = ShopMenuService.getGoodsDetail(shopid,cid,goodsid);
                goods_list.push({cid:cid,name:detail.name,goods:[detail.goods]});
            }else{
                var good = null;
                for (var j=0; j<goodsitem.goods.length; j++) {
                    if (goodsitem.goods[j].goodsid == goodsid) {
                        good = goodsitem.goods[j];
                        break;
                    }
                }
                var detail = ShopMenuService.getGoodsDetail(shopid,cid, goodsid);
                if(good == null){
                    goodsitem.goods.push(detail.goods);
                }else{
                    good.buycount = detail.goods.buycount;
                }
            }
            //
            shop_goods_list[shopid] = goods_list;
            $storage.cmtShopcart[shopid] = goods_list;
        }

        //从购物车减少购买量
        this.removeGoods = function(shopid, cid, goodsid){
            //console.log('removeGoods('+shopid+','+goodsid+')');
            ShopMenuService.removeGood(shopid, cid, goodsid);

            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            for (var i=0; i<goods_list.length; i++) {
                var isOK = false;
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid) {
                    var indextodelete = -1;
                    for (var j=0; j<goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        if (good.goodsid == goodsid) {
                            if (good.buycount < 1) {
                                indextodelete = j;
                            }else{
                                var detail = ShopMenuService.getGoodsDetail(shopid,cid, goodsid);
                                good.buycount = detail.goods.buycount;
                                if (good.buycount < 1) {
                                    indextodelete = j;
                                }
                            }
                            break;
                        }
                    }
                    if (indextodelete >= 0) {
                        goodsitem.goods.splice(indextodelete, 1);
                    }
                    isOK = true;
                }
                // clean catagory
                if (goodsitem.goods.length == 0) {
                    goods_list.splice(i, 1);
                }
                // complete then break
                if (isOK) {
                    break;
                }
            }
            //clean shop if empty
            if (goods_list.length == 0) {
                delete shop_goods_list[shopid];
                delete $storage.cmtShopcart[shopid];
            }
            //
            else{
                shop_goods_list[shopid] = goods_list;
                $storage.cmtShopcart[shopid] = goods_list;
            }
        }

        //输入大于零绑定到shop_goods_list
        this.importGoods = function(shopid, cid, goodsid,buycount){
            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goods_list.length;i++){
                if(goods_list[i].cid == cid){
                    goodsitem = goods_list[i];
                    break;
                }
            }
            if(goodsitem == null){
                detail = ShopMenuService.getGoodsDetail(shopid,cid,goodsid);
                goods_list.push({cid:cid,name:detail.name,goods:[detail.goods]});
            }else{
                var good = null;
                for (var j=0; j<goodsitem.goods.length; j++) {
                    if (goodsitem.goods[j].goodsid == goodsid) {
                        good = goodsitem.goods[j];
                        break;
                    }
                }
                if(good == null){
                    var detail = ShopMenuService.getGoodsDetail(shopid,cid, goodsid);
                    goodsitem.goods.push(detail.goods);
                }else {
                    if(buycount>999){
                        buycount = 999;
                        good.buycount = buycount;
                        input_alert('超出购买上限');
                    }
                    else{
                        good.buycount = buycount;
                    }
                }
            }
            for (var i=0; i<goods_list.length; i++) {
                var isOK = false;
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid) {
                    var indextodelete = -1;
                    for (var j=0; j<goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        if (good.goodsid == goodsid) {
                            if (good.buycount < 1||good.buycount == undefined) {
                                indextodelete = j;
                            }
                            break;
                        }
                    }
                    if (indextodelete >= 0) {
                        goodsitem.goods.splice(indextodelete, 1);
                    }
                    isOK = true;
                }
                // clean catagory
                if (goodsitem.goods.length == 0) {
                    goods_list.splice(i, 1);
                }
                // complete then break
                if (isOK) {
                    break;
                }
            }
            //clean shop if empty
            if (goods_list.length == 0) {
                delete shop_goods_list[shopid];
                delete $storage.cmtShopcart[shopid];
            }
            //
            else{
                shop_goods_list[shopid] = goods_list;
                $storage.cmtShopcart[shopid] = goods_list;
            }
        }

        //获取全部购物清单
        this.getAllShopCarInfo = function(){
            var all_goods_list = [];
            for(var shopid in shop_goods_list) {
                var goodscontent = this.gettotalpriceandcount(shopid);
                var shopinfo = ShopService.queryShopByShopid(shopid);
                var shop_goods = {shopid:shopid, shop:shopinfo, list:shop_goods_list[shopid], goodscontent:goodscontent};
                all_goods_list.push(shop_goods);
            }
            return all_goods_list;
        }

        //获取店铺购物列表
        this.getShopCarByShopid = function(shopid){
            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            return goods_list;
        }

        //获取总价和产品总数
        this.gettotalpriceandcount = function(shopid){
            var count = 0;
            var integration=0;
            var totalprice = 0;
            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            //
            for (var i=0; i<goods_list.length; i++) {
                var goodsitem = goods_list[i];
                for (var j=0; j<goodsitem.goods.length; j++) {
                    var good = goodsitem.goods[j];
                    if(good.buycount>0){
                    totalprice += good.price * good.buycount;
                    count += 1*good.buycount;
                    integration += (good.price * good.buycount)/100;
                }
                }
                totalprice=changeDecimal(totalprice,100);
                integration=changeDecimal(integration,10000);
            }
            return {totalprice:totalprice,count:count,integration:integration};
        };
        //获取总价和产品总数
        this.getallshoppriceandcount = function(){
            var AllShopCarInfo = this.getAllShopCarInfo();
            var totalprice =0,count=0,integration=0;
            for(var i=0;i<AllShopCarInfo.length;i++){
                totalprice +=AllShopCarInfo[i].goodscontent.totalprice;
                count +=AllShopCarInfo[i].goodscontent.count;
                integration +=AllShopCarInfo[i].goodscontent.integration;
            }
            return {totalprice:totalprice,count:count,integration:integration};
        };

        //根据id移除某个商品
        this.cleanById = function(shopid, cataid, goodsid){
            ShopMenuService.cleanGoodById(shopid, cataid, goodsid);

            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            for (var i=0; i<goods_list.length; i++) {
                var isOK = false;
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cataid) {
                    var indextodelete = -1;
                    for (var j=0; j<goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        if (good.goodsid == goodsid) {
                            indextodelete = j;
                            break;
                        }
                    }
                    if (indextodelete >= 0) {
                        goodsitem.goods.splice(indextodelete, 1);
                    }
                    isOK = true;
                }
                // clean catagory
                if (goodsitem.goods.length == 0) {
                    goods_list.splice(i, 1);
                }
                // complete then break
                if (isOK) {
                    break;
                }
            }
            //clean shop if empty
            if (goods_list.length == 0) {
                delete shop_goods_list[shopid];
                delete $storage.cmtShopcart[shopid];
            }
        }

        //移除某个商店id下的所有商品
        this.cleanByShopId = function(shopid){
            ShopMenuService.removeShopById(shopid);
            if(shop_goods_list[shopid]){
                delete shop_goods_list[shopid]; //将这个商店id下面所有的商品删除
            }
        }

        //获取商品购买数量
        this.getBuycountByGoodsid = function(shopid, cataid, goodsid) {
            var goods_list = [];
            if(shop_goods_list[shopid]){
                goods_list = shop_goods_list[shopid];
            }
            for (var i=0; i<goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cataid == cataid) {
                    for (var j=0; j<goodsitem.goods.length; j++) {
                        var good = goodsitem.goods[j];
                        if (good.goodsid == goodsid) {
                            return good.buycount;
                        }
                    }
                }
            }
            return 0;
        }
        //同步本地缓存和购物车数据
        this.synchronizationStorage = function () {
            $storage.cmtShopcart = shop_goods_list;
        }
    }])
